﻿using System;
using System.Collections.Generic;
using System.Text;

namespace zszs
{
    /// <summary>
    /// post 的数据
    /// </summary>
    public class PostData
    {
        private IDictionary<string, string> _dictionary { get; set; }
        public PostData(IDictionary<string, string> dictionary)
        {
            this._dictionary = dictionary;
        }

        /// <summary>
        /// 生成需要POST的数据
        /// </summary>
        /// <returns></returns>
        public new string ToString()
        {
            return this.ToString(Encoding.UTF8);
        }
        /// <summary>
        /// 生成需要POST的数据
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public string ToString(Encoding e)
        {
            System.Text.StringBuilder sb = new StringBuilder(_dictionary.Count * 10);
            int i = 0;
            foreach (var item in _dictionary.Keys)
            {
                sb.Append(item + "=" + System.Web.HttpUtility.UrlEncode(_dictionary[item], e) + "&");
            }
            return sb.ToString().TrimEnd('&');
        }

        /// <summary>
        /// 根据 字符串 生成 相应的 代码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public string ToString(string postData)
        {
            string[] str = postData.Split('&');
            StringBuilder sb = new StringBuilder(str.Length * 10);
            sb.Append("IDictionary<string, string> dictionary = new Dictionary<string, string>();"+Environment.NewLine);
            for (int i = 0; i < str.Length; i++)
            {
                string[] s = str[i].Split('=');
                sb.Append(string.Format("dictionary.Add({0}{1}{0},{3});{2}", "\"", s[0], Environment.NewLine, "new MatchData(\"\\\"" + s[0] + "\\\":(\\\"?)\", \"(\\\"?)\", resultInfo).Match(RegexOptions.None)"));
            }
            return sb.ToString();
        }
           
    }
}
